import { BASE_URL, USER_SIGN_STATUS } from "@/config.js"
import { USER_LOCAL_ADDRESS, USER_SHOP_CART_ADDRESS } from '@/storage.js'
import { Banner, noticePage, HotList } from "@/api/base.js"
import { userBalance, userData } from '@/api/personal.js'
import { cartCount } from '@/api/user/shopcart.js'

export default {
	state: {
		localAddress: {},
		shopcartAddress: {},
		
		bannerList: [],
		
		noticeList: [],
		hotWordList: [],
		userData: {},
		scoreBalance: {},
	},
	mutations: {
		// 更新用户首页轮播图
		UPDATE_USER_INDEX_BANNER(state, data) {
			state.bannerList = data
		},
		// 更新用户首页公告列表
		UPDATE_USER_INDEX_NOTICE(state, data) {
			state.noticeList = data
		},
		// 更新热刺列表
		UPDATE_USER_HOT_WORD_LIST(state, data) {
			state.hotWordList = data
		},
		// 更新地址信息
		UPDATE_USER_LOCAL_ADDRESS(state, data) {
			uni.setStorageSync(USER_LOCAL_ADDRESS, JSON.stringify(data))
			state.localAddress = { ...data }
		},
		// 更新购物车地址
		UPDATE_USER_SHOP_CART_ADDRESS(state, data) {
			uni.setStorageSync(USER_SHOP_CART_ADDRESS, JSON.stringify(data))
			state.shopcartAddress = { ...data }
		},
		// 更新收藏、拼团
		UPDATE_USER_DATA(state, data) {
			state.userData = data
		}
	},
	actions: {
		// 初始化轮播图
		initBanner({ state, commit }, data) {
			Banner({ type: 4 }).then((result) => {
				commit('UPDATE_USER_INDEX_BANNER', result.map(item => ({ ...item, image: BASE_URL + item.imgUrl })))
			})
		},
		// 获取首页公告信息
		initNotice({ state, commit }, data) {
			noticePage({ pageNumber: 1, pageSize: 10 }).then((result) => {
				commit('UPDATE_USER_INDEX_NOTICE', result.list)
			})
		},
		// 获取热词列表
		initHotList({ state, commit }, data) {
			return HotList({}).then((result) => {
				commit('UPDATE_USER_HOT_WORD_LIST', result)
			})
		},
		// 初始化收藏数、待拼团数
		initUserData({ state, commit }, data) {
			userData({}).then(result => {
				commit('UPDATE_USER_DATA', result)
			})
		}
	}
}
